package com.sunday.electric.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunday.electric.R;
import com.sunday.electric.base.BaseFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/1/9.
 */
public class PlainFragment extends BaseFragment {
    @Bind(R.id.pager)
    ViewPager viewPager;
    @Bind(R.id.tab_layout)
    TabLayout tabLayout;

    private PlainListFragment[]   tabFragment = new PlainListFragment[3];
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_common_tab,null);
        ButterKnife.bind(this,rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        for(int i= 0 ;i<tabFragment.length;i++){
            tabFragment[i] = new PlainListFragment();
        }
        viewPager.setAdapter(new SampleFragmentPagerAdapter(getChildFragmentManager(),
                mContext));
        tabLayout.setupWithViewPager(viewPager);
    }



    class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
        private String[] tabs;

        public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            tabs = context.getResources().getStringArray(R.array.plain_title);
        }

        @Override
        public int getCount() {
            return tabs.length;
        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();
            int type = 0;
            switch (position){
                case 0:
                    type = 1;//待验证
                    break;
                case 1:
                    type = 2;//已完成
                    break;
            }
            bundle.putInt("status", type);
            tabFragment[position].setArguments(bundle);
            return tabFragment[position];

        }

        @Override
        public CharSequence getPageTitle(int position) {
            // Generate title based on item position
            return tabs[position];
        }
    }
}
